<template>

  <div>
    <!-- 面包屑组件 -->
    <breadcrumb :nav-data="['商品管理', '商品分类']"></breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!--添加分类 -->
      <el-button type="primary" @click="showAddGoodsDialog">添加分类</el-button>
      <!-- 分类列表 -->
      <el-table :data="tableData" border row-key="cat_id" style="width: 100%">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="cat_name" label="分类名称"></el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效">
          <template slot-scope="scope">
            <i
              style="color:green"
              v-if="!scope.row.cat_deleted"
              class="el-icon-success"
            ></i>
            <i style="color:red" v-else class="el-icon-error"></i>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="排序">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.cat_level === 0" effect="dark"
              >一级</el-tag
            >
            <el-tag
              size="mini"
              v-if="scope.row.cat_level === 1"
              type="success"
              effect="dark"
              >二级</el-tag
            >
            <el-tag
              size="mini"
              v-if="scope.row.cat_level === 2"
              type="warning"
              effect="dark"
              >三级</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              size="mini"
              @click="onEditCate(scope.row)"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <!-- 删除 -->
            <el-button
              type="danger"
              size="mini"
              @click="onDeleteCate(scope.row)"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total,sizes,prev,pager,next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加商品分类 -->
    <el-dialog title="添加商品分类" :visible.sync="addGoodsCateVisible">
      <el-form
        :model="addGoodsCate"
        :rules="addGoodsCateRules"
        ref="addGoodsCateForm"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addGoodsCate.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectKeys"
            @change="onCascaderChange"
            :options="cascaderOptions"
            :props="cascaderProps"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addGoodsCateVisible = false">取消</el-button>
        <el-button type="primary" @click="onAddGoodsCate">确定</el-button>
      </div>
    </el-dialog>
    <!-- 修改分类对话框 -->
    <el-dialog title="修改名称" :visible.sync="editDialogVisible" width="60%">
      <el-form
        :model="editForm"
        :rules="editRules"
        ref="editFormRef"
        label-width="100px"
      >
        <!-- 分类名称 -->
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onEditBtnClick">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getCategoriesList();
  },
  data() {
    return {
      // 查询所有分类的参数
      queryInfo: {
        type: "",
        pagenum: 1,
        pagesize: 10,
      },
      //总条数
      total: 0,
      tableData: [],
      // 显示隐藏商品分类对话框
      addGoodsCateVisible: false,
      // 添加商品分类
      addGoodsCate: {
        cat_pid: "0",
        cat_name: "",
        cat_level: "0",
      },
      // 添加商品分类验证
      addGoodsCateRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在2到8个字符", trigger: "blur" },
        ],
      },
      // 级联选择器
      cascaderOptions: [],
      // 级联选择器配置顶
      cascaderProps: {
        checKStrictly: true,
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      // 级联选择器数据绑定
      selectKeys: [],
      editDialogVisible: false,
      // 编辑分类
      editForm: {
        cat_name: "",
      },
      editRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在2到8个字符", trigger: "blur" },
        ],
      },
      curEditId:"",
    };
  },
  methods:{
    //获取分类列表
    async getCategoriesList(){
      let {data:res}= await this.$http.get("categories",{
        params:this.queryInfo,
      });
      if(res.meta.status !==200) return this.$message.error(res.meta.msg);
      this.total=res.data.total;
      this.tableData=res.data.result;
      // console.log(res);
    },
    handleSizeChange(val){
      this.queryInfo.pagesize=val;
      this.getCategoriesList();
    },
    handleCurrentChange(val){
      this.queryInfo.pagenum=val;
      this.getCategoriesList();
    },
    // 显示添加分类对话框
    async showAddGoodsDialog(){
      let{data:res} = await this.$http.get("categories",{
        params:{type:2},
      });
      if(res.meta.status !==200) return this.$message.error(res.meta.msg);
      this.cascaderOptions=res.data;
      this.addGoodsCateVisible=true;
    },
    // 级联选择器change事件
    onCascaderChange(){
      if(this.selectKeys.length){
        this.addGoodsCate.cat_pid=this.selectKeys[this.selectKeys.length -1];
        this.addGoodsCate.cat_level=this.selectKeys.length;
      }else{
        this.addGoodsCate.cat_pid="0";
        this.addGoodsCate.cat_level="0";
      }
    },
    //确定添加分类
    onAddGoodsCate(){
      this.$refs.addGoodsCateForm.validate(async (valid)=>{
        if(!valid) return;
        let {data:res} = await this.$http.post(
          "categories",
          this.addGoodsCate
        );
        this.addGoodsCate.cat_name="";
        this.selectKeys=[];
        if(res.meta.status !==201) return this.$message.error(res.meta.msg);
        this.getCategoriesList();
        this.$message.success(res.meta.msg);
        this.addGoodsCateVisible = false;
      });
    },
    // 删除
    async onDeleteCate(row){
      let result = await this.$confirm(
        "此操作将永久删除分类,是否继续",
        "提示",
        {
          confirmButtonText:"确定",
          cancelButtonText:"取消",
          type:"warning",
        }
      ).catch((err)=>err);
      if(result !=="confirm") return this.$message.info("取消删除");
      let {data:res} = await this.$http.delete(`categories/${row.cat_id}`);
      if(res.meta.status !==200) return this.$message.error(res.meta.msg);
      this.getCategoriesList();
      this.$message.success(res.meta.msg);
    },
    // 编辑
    onEditCate(item){
      this.curEditId=item.cat_id;
      this.editForm.cat_name=item.cat_name;
      this.editDialogVisible = true;
    },
    // 确认修改
    onEditBtnClick(){
      this.$refs.editFormRef.validate(async (value)=>{
        if(!value) return;
        let {data:res} = await this.$http.put(
          `categories/${this.curEditId}`,
          this.editForm
        );
        if(res.meta.status !==200) return this.$message.error(res.meta.msg);
        this.getCategoriesList();
        this.$message.success(res.meta.msg);
        this.editDialogVisible=false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
}

.el-cascader{
  width: 100%;
}
</style>
